home *** CD-ROM | disk | FTP | other *** search
/ USA Bestseller / USA BESTSELLER Vol 1-95 (Hepp-Computer)(1995).iso / e140 / environ.c < prev    next >
C/C++ Source or Header  |  1993-06-04  |  14KB  |  742 lines

  1. /* ENVIRON.C, UNARJ, R JUNG, 09/01/91
  2.  * Implementation dependent routines
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   The UNIX file date-time stamping code is derived from ZOO by
  18.  *   Rahul Dhesi.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/09/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler    Portabilized.
  24.  * 04/29/91  R. Jung     Added get_mode_str().
  25.  * 05/08/91  R. Jung     Combined set_ftime() and set_fmode().
  26.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  27.  *                       set_ftime_mode().
  28.  * 07/07/91  R. Jung     Added default_case_path() and UNIX section.
  29.  * 07/24/91  R. Jung     Fixed use of _chmod to handle directories.
  30.  * 08/27/91  R. Jung     Added date/time handling to Coherent.
  31.  * 09/01/91  R. Jung     Added #include <stdlib.h> to vanilla section.
  32.  *                       Added file date-time stamping to UNIX section.
  33.  *
  34.  */
  35.  
  36. #include "unarj.h"
  37.  
  38. #ifdef __TURBOC__
  39.  
  40. #define SUBS_DEFINED
  41.  
  42. #include <string.h>
  43. #include <dos.h>
  44. #include <io.h>
  45. #include <fcntl.h>
  46. #include <alloc.h>
  47.  
  48. FILE *
  49. file_open(name, mode)
  50. char *name;
  51. char *mode;
  52. {
  53.     return fopen(name, mode);
  54. }
  55.  
  56. int
  57. file_read(buf, size, nitems, stream)
  58. char *buf;
  59. int  size;
  60. int  nitems;
  61. FILE *stream;
  62. {
  63.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  64. }
  65.  
  66. int
  67. file_seek(stream, offset, mode)
  68. FILE *stream;
  69. long offset;
  70. int  mode;
  71. {
  72.     return fseek(stream, offset, mode);
  73. }
  74.  
  75. long
  76. file_tell(stream)
  77. FILE *stream;
  78. {
  79.     return ftell(stream);
  80. }
  81.  
  82. int
  83. file_write(buf, size, nitems, stream)
  84. char *buf;
  85. int  size;
  86. int  nitems;
  87. FILE *stream;
  88. {
  89.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  90. }
  91.  
  92. voidp *
  93. xmalloc(size)
  94. int size;
  95. {
  96.     return (voidp *)malloc((size_t) size);
  97. }
  98.  
  99. void
  100. case_path(name)
  101. char *name;
  102. {
  103.     strupper(name);
  104. }
  105.  
  106. void
  107. default_case_path(name)
  108. char *name;
  109. {
  110.     strupper(name);
  111. }
  112.  
  113. int
  114. file_exists(name)
  115. char *name;
  116. {
  117.     return (access(name, 0) == 0);
  118. }
  119.  
  120. void
  121. get_mode_str(str, mode)
  122. char *str;
  123. uint mode;
  124. {
  125.     strcpy(str, "---W");
  126.     if (mode & FA_ARCH)
  127.         str[0] = 'A';
  128.     if (mode & FA_SYSTEM)
  129.         str[1] = 'S';
  130.     if (mode & FA_HIDDEN)
  131.         str[2] = 'H';
  132.     if (mode & FA_RDONLY)
  133.         str[3] = 'R';
  134. }
  135.  
  136. int
  137. set_ftime_mode(name, tstamp, attribute, host)
  138. char  *name;
  139. ulong tstamp;
  140. uint  attribute;
  141. uint  host;
  142. {
  143.     FILE *fd;
  144.     int code;
  145.  
  146.     if ((fd = fopen(name, "r+b")) == NULL)
  147.         return -1;
  148.     code = setftime(fileno(fd), (struct ftime *) &tstamp);
  149.     fclose(fd);
  150.     if (host == OS)
  151.     {
  152.         attribute &= 0x27;
  153.         if (_chmod(name, 1, attribute) == -1)
  154.             return -1;
  155.     }
  156.     return code;
  157. }
  158.  
  159. #endif
  160.  
  161. #ifdef _QC
  162.  
  163. #define SUBS_DEFINED
  164.  
  165. #include <string.h>
  166. #include <dos.h>
  167. #include <io.h>
  168. #include <fcntl.h>
  169. #include <malloc.h>
  170.  
  171. FILE *
  172. file_open(name, mode)
  173. char *name;
  174. char *mode;
  175. {
  176.     return fopen(name, mode);
  177. }
  178.  
  179. int
  180. file_read(buf, size, nitems, stream)
  181. char *buf;
  182. int  size;
  183. int  nitems;
  184. FILE *stream;
  185. {
  186.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  187. }
  188.  
  189. int
  190. file_seek(stream, offset, mode)
  191. FILE *stream;
  192. long offset;
  193. int  mode;
  194. {
  195.     return fseek(stream, offset, mode);
  196. }
  197.  
  198. long
  199. file_tell(stream)
  200. FILE *stream;
  201. {
  202.     return ftell(stream);
  203. }
  204.  
  205. int
  206. file_write(buf, size, nitems, stream)
  207. char *buf;
  208. int  size;
  209. int  nitems;
  210. FILE *stream;
  211. {
  212.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  213. }
  214.  
  215. voidp *
  216. xmalloc(size)
  217. int size;
  218. {
  219.     return (voidp *)malloc((size_t) size);
  220. }
  221.  
  222. void
  223. case_path(name)
  224. char *name;
  225. {
  226.     strupper(name);
  227. }
  228.  
  229. void
  230. default_case_path(name)
  231. char *name;
  232. {
  233.     strupper(name);
  234. }
  235.  
  236. int
  237. file_exists(name)
  238. char *name;
  239. {
  240.     return (access(name, 0) == 0);
  241. }
  242.  
  243. void
  244. get_mode_str(str, mode)
  245. char *str;
  246. uint mode;
  247. {
  248.     strcpy(str, "---W");
  249.     if (mode & FA_ARCH)
  250.         str[0] = 'A';
  251.     if (mode & FA_SYSTEM)
  252.         str[1] = 'S';
  253.     if (mode & FA_HIDDEN)
  254.         str[2] = 'H';
  255.     if (mode & FA_RDONLY)
  256.         str[3] = 'R';
  257. }
  258.  
  259. int
  260. set_ftime_mode(name, tstamp, attribute, host)
  261. char  *name;
  262. ulong tstamp;
  263. uint  attribute;
  264. uint  host;
  265. {
  266.     FILE *fd;
  267.     int code;
  268.     uint date_stamp, time_stamp;
  269.  
  270.     date_stamp = (uint)(tstamp >> 16);
  271.     time_stamp = (uint)(tstamp & 0xFFFF);
  272.     if ((fd = fopen(name, "r+b")) == NULL)
  273.         return -1;
  274.     code = _dos_setftime(fileno(fd), date_stamp, time_stamp);
  275.     fclose(fd);
  276.     if (host == OS)
  277.     {
  278.         if (_dos_setfileattr(name, attribute))
  279.             return -1;
  280.     }
  281.     return code;
  282. }
  283.  
  284. #endif
  285.  
  286. #ifdef _OS2
  287.  
  288. #define SUBS_DEFINED
  289.  
  290. #include <string.h>
  291. #define INCL_DOSFILEMGR
  292. #include <os2.h>
  293. #include <io.h>
  294. #include <fcntl.h>
  295.  
  296. FILE *
  297. file_open(name, mode)
  298. char *name;
  299. char *mode;
  300. {
  301.     return fopen(name, mode);
  302. }
  303.  
  304. int
  305. file_read(buf, size, nitems, stream)
  306. char *buf;
  307. int  size;
  308. int  nitems;
  309. FILE *stream;
  310. {
  311.     return fread(buf, (size_t) size, (size_t) nitems, stream);
  312. }
  313.  
  314. int
  315. file_seek(stream, offset, mode)
  316. FILE *stream;
  317. long offset;
  318. int  mode;
  319. {
  320.     return fseek(stream, offset, mode);
  321. }
  322.  
  323. long
  324. file_tell(stream)
  325. FILE *stream;
  326. {
  327.     return ftell(stream);
  328. }
  329.  
  330. int
  331. file_write(buf, size, nitems, stream)
  332. char *buf;
  333. int  size;
  334. int  nitems;
  335. FILE *stream;
  336. {
  337.     return fwrite(buf, (size_t) size, (size_t) nitems, stream);
  338. }
  339.  
  340. voidp *
  341. xmalloc(size)
  342. int size;
  343. {
  344.     return (voidp *)malloc((size_t) size);
  345. }
  346.  
  347. void
  348. case_path(name)
  349. char *name;
  350. {
  351.     strupper(name);
  352. }
  353.  
  354. void
  355. default_case_path(name)
  356. char *name;
  357. {
  358.     strupper(name);
  359. }
  360.  
  361. int
  362. file_exists(name)
  363. char *name;
  364. {
  365.     return (access(name, 0) == 0);
  366. }
  367.  
  368. void
  369. get_mode_str(str, mode)
  370. char *str;
  371. uint mode;
  372. {
  373.     strcpy(str, "---W");
  374.     if (mode & FA_ARCH)
  375.         str[0] = 'A';
  376.     if (mode & FA_SYSTEM)
  377.         str[1] = 'S';
  378.     if (mode & FA_HIDDEN)
  379.         str[2] = 'H';
  380.     if (mode & FA_RDONLY)
  381.         str[3] = 'R';
  382. }
  383.  
  384. int
  385. set_ftime_mode(name, tstamp, attribute, host)
  386. char  *name;
  387. ulong tstamp;
  388. uint  attribute;
  389. uint  host;
  390. {
  391.     int code;
  392.     FDATE date_stamp;
  393.     FTIME time_stamp;
  394.     HFILE handle;
  395.     FILESTATUS info;
  396.     USHORT action;
  397.  
  398.     date_stamp.day = ts_day (tstamp);
  399.     date_stamp.month = ts_month (tstamp);
  400.     date_stamp.year = ts_year (tstamp) - 1980;
  401.     time_stamp.twosecs = ts_sec (tstamp) / 2;
  402.     time_stamp.minutes = ts_min (tstamp);
  403.     time_stamp.hours = ts_hour (tstamp);
  404.     if (DosOpen (name, &handle, &action, 0L, 0, FILE_OPEN,
  405.                  OPEN_ACCESS_READWRITE|OPEN_SHARE_DENYREADWRITE, 0L) != 0)
  406.         return -1;
  407.     info.fdateCreation = date_stamp;
  408.     info.ftimeCreation = time_stamp;
  409.     info.fdateLastAccess = date_stamp;
  410.     info.ftimeLastAccess = time_stamp;
  411.     info.fdateLastWrite = date_stamp;
  412.     info.ftimeLastWrite = time_stamp;
  413.     info.cbFile = 0;
  414.     info.cbFileAlloc = 0;
  415.     info.attrFile = 0;
  416.     code = (int)DosSetFileInfo (handle, 1, (PBYTE)&info, sizeof (info));
  417.     (void)DosClose (handle);
  418.     if (host == OS)
  419.     {
  420.         if (DosSetFileMode (name, attribute, 0L))
  421.             return -1;
  422.     }
  423.     return code;
  424. }
  425.  
  426. #endif
  427.  
  428. #ifdef UNIX
  429.  
  430. #define SUBS_DEFINED
  431.  
  432. #include <time.h>
  433.  
  434. #ifndef time_t
  435. #define time_t long
  436. #endif
  437.  
  438. extern struct tm *localtime();
  439. extern time_t time();
  440. extern char   *strcpy();
  441. extern voidp  *malloc();
  442.  
  443. FILE *
  444. file_open(name, mode)
  445. char *name;
  446. char *mode;
  447. {
  448.     return fopen(name, mode);
  449. }
  450.  
  451. int
  452. file_read(buf, size, nitems, stream)
  453. char *buf;
  454. int  size;
  455. int  nitems;
  456. FILE *stream;
  457. {
  458.     return fread(buf, (int) size, (int) nitems, stream);
  459. }
  460.  
  461. int
  462. file_seek(stream, offset, mode)
  463. FILE *stream;
  464. long offset;
  465. int  mode;
  466. {
  467.     return fseek(stream, offset, mode);
  468. }
  469.  
  470. long
  471. file_tell(stream)
  472. FILE *stream;
  473. {
  474.     return ftell(stream);
  475. }
  476.  
  477. int
  478. file_write(buf, size, nitems, stream)
  479. char *buf;
  480. int  size;
  481. int  nitems;
  482. FILE *stream;
  483. {
  484.     return fwrite(buf, (int) size, (int) nitems, stream);
  485. }
  486.  
  487. voidp *
  488. xmalloc(size)
  489. int size;
  490. {
  491.     return (voidp *)malloc((uint) size);
  492. }
  493.  
  494. void
  495. case_path(name)
  496. char *name;
  497. {
  498.     (char *) name;
  499. }
  500.  
  501. void
  502. default_case_path(name)
  503. char *name;
  504. {
  505.     strlower(name);
  506. }
  507.  
  508. int
  509. file_exists(name)
  510. char *name;
  511. {
  512.     FILE *fd;
  513.  
  514.     if ((fd = fopen(name, "rb")) == NULL)
  515.         return 0;
  516.     fclose(fd);
  517.     return 1;
  518. }
  519.  
  520. void
  521. get_mode_str(str, mode)
  522. char *str;
  523. uint mode;
  524. {
  525.     strcpy(str, "---W");
  526.     if (mode & FA_ARCH)
  527.         str[0] = 'A';
  528.     if (mode & FA_SYSTEM)
  529.         str[1] = 'S';
  530.     if (mode & FA_HIDDEN)
  531.         str[2] = 'H';
  532.     if (mode & FA_RDONLY)
  533.         str[3] = 'R';
  534. }
  535.  
  536. long
  537. gettz()         /* returns the offset from GMT in seconds */
  538. {
  539. #define NOONOFFSET    43200L
  540. #define SEC_IN_DAY    (24L * 60L * 60L)
  541. #define INV_VALUE     (SEC_IN_DAY + 1L)
  542.     static long retval = INV_VALUE;
  543.     long now, noon;
  544.     struct tm *noontm;
  545.  
  546.     if (retval != INV_VALUE)
  547.         return retval;
  548.     now = (long) time((long *) 0);
  549.     /* Find local time for GMT noon today */
  550.     noon = now - now % SEC_IN_DAY + NOONOFFSET ;
  551.     noontm = localtime(&noon);
  552.     retval = NOONOFFSET - 60 * (60 * noontm->tm_hour - noontm->tm_min);
  553.     return retval;
  554. }
  555.  
  556. long
  557. mstonix(tstamp)
  558. ulong tstamp;
  559. {
  560.     uint date, time;
  561.     int year, month, day, hour, min, sec, daycount;
  562.     long longtime;
  563.     /* no. of days to beginning of month for each month */
  564.     static int dsboy[12] =
  565.         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  566.  
  567.     date = (uint) ((tstamp >> 16) & 0xffff);
  568.     time = (uint) (tstamp & 0xffff);
  569.     if (date == 0 && time == 0)
  570.         return 0L;
  571.  
  572.     year  = ((date >> 9) & 0x7f) + 1980;
  573.     month = (date >> 5) & 0x0f;
  574.     day   = date & 0x1f;
  575.     hour  = (time >> 11) & 0x1f;
  576.     min   = (time >> 5) & 0x3f;
  577.     sec   = (time & 0x1f) * 2;
  578.  
  579.     daycount = 365 * (year - 1970) +   /* days due to whole years */
  580.                (year - 1969) / 4 +     /* days due to leap years */
  581.                dsboy[month-1] +        /* days since beginning of this year */
  582.                day-1;                  /* days since beginning of month */
  583.  
  584.     if (year % 4 == 0 &&
  585.         year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
  586.         daycount++;                     /* is March or later, add a day */
  587.  
  588.     longtime = daycount * 24L * 60L * 60L +
  589.                hour * 60L * 60L + min * 60 + sec;
  590.     return longtime;
  591. }
  592.  
  593. int
  594. set_ftime_mode(name, tstamp, attribute, host)
  595. char  *name;
  596. ulong tstamp;
  597. uint  attribute;
  598. uint  host;
  599. {
  600.     time_t m_time;
  601.     struct utimbuf
  602.     {
  603.        time_t atime;             /* New access time */
  604.        time_t mtime;             /* New modification time */
  605.     } tb;
  606.  
  607.     (char *) name;
  608.     (uint) attribute;
  609.     (uint) host;
  610.  
  611.     m_time = mstonix(tstamp) + gettz();
  612.  
  613.     tb.mtime = m_time;                  /* Set modification time */
  614.     tb.atime = m_time;                  /* Set access time */
  615.  
  616.     /* set the time stamp on the file */
  617.     return utime(name, &tb);
  618. }
  619.  
  620. #endif  /* end of UNIX section */
  621.  
  622. #ifndef SUBS_DEFINED       /* vanilla version for other compilers */
  623.  
  624. #ifdef MODERN
  625. #include <string.h>
  626. #include <stdlib.h>
  627. #else /* !MODERN */
  628. extern char *strcpy();
  629. extern voidp *malloc();
  630. #endif /* ?MODERN */
  631.  
  632. FILE *
  633. file_open(name, mode)
  634. char *name;
  635. char *mode;
  636. {
  637.     return fopen(name, mode);
  638. }
  639.  
  640. int
  641. file_read(buf, size, nitems, stream)
  642. char *buf;
  643. int  size;
  644. int  nitems;
  645. FILE *stream;
  646. {
  647.     return fread(buf, (int) size, (int) nitems, stream);
  648. }
  649.  
  650. int
  651. file_seek(stream, offset, mode)
  652. FILE *stream;
  653. long offset;
  654. int  mode;
  655. {
  656.     return fseek(stream, offset, mode);
  657. }
  658.  
  659. long
  660. file_tell(stream)
  661. FILE *stream;
  662. {
  663.     return ftell(stream);
  664. }
  665.  
  666. int
  667. file_write(buf, size, nitems, stream)
  668. char *buf;
  669. int  size;
  670. int  nitems;
  671. FILE *stream;
  672. {
  673.     return fwrite(buf, (int) size, (int) nitems, stream);
  674. }
  675.  
  676. voidp *
  677. xmalloc(size)
  678. int size;
  679. {
  680.     return (voidp *)malloc((uint) size);
  681. }
  682.  
  683. void
  684. case_path(name)
  685. char *name;
  686. {
  687.     (char *) name;
  688. }
  689.  
  690. void
  691. default_case_path(name)
  692. char *name;
  693. {
  694.     (char *) name;
  695. }
  696.  
  697. int
  698. file_exists(name)
  699. char *name;
  700. {
  701.     FILE *fd;
  702.  
  703.     if ((fd = fopen(name, "rb")) == NULL)
  704.         return 0;
  705.     fclose(fd);
  706.     return 1;
  707. }
  708.  
  709. void
  710. get_mode_str(str, mode)
  711. char *str;
  712. uint mode;
  713. {
  714.     strcpy(str, "---W");
  715.     if (mode & FA_ARCH)
  716.         str[0] = 'A';
  717.     if (mode & FA_SYSTEM)
  718.         str[1] = 'S';
  719.     if (mode & FA_HIDDEN)
  720.         str[2] = 'H';
  721.     if (mode & FA_RDONLY)
  722.         str[3] = 'R';
  723. }
  724.  
  725. int
  726. set_ftime_mode(name, tstamp, attribute, host)
  727. char  *name;
  728. ulong tstamp;
  729. uint  attribute;
  730. uint  host;
  731. {
  732.     (char *) name;
  733.     (ulong) tstamp;
  734.     (uint) attribute;
  735.     (uint) host;
  736.     return 0;
  737. }
  738.  
  739. #endif  /* end of vanilla section */
  740.  
  741. /* end ENVIRON.C */
  742.